/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.freezingassets.service;

import java.util.ArrayList;
import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.subgoods.entity.SubGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.freezingassets.entity.FreezingAssets;
import com.jeeplus.modules.freezingassets.mapper.FreezingAssetsMapper;

/**
 * 冻结资产Service
 * @author w
 * @version 2021-03-26
 */
@Service
@Transactional(readOnly = true)
public class FreezingAssetsService extends CrudService<FreezingAssetsMapper, FreezingAssets> {
	@Autowired
	private MemberService memberService;

	public FreezingAssets get(String id) {
		return super.get(id);
	}
	
	public List<FreezingAssets> findList(FreezingAssets freezingAssets) {
		return super.findList(freezingAssets);
	}
	
	public Page<FreezingAssets> findPage(Page<FreezingAssets> page, FreezingAssets freezingAssets) {
		String managerId = "";
		List<FreezingAssets> accountList = new ArrayList<>();
		dataRuleFilter(freezingAssets);
		freezingAssets.setPage(page);
		if (freezingAssets.getManager() != null){
			managerId = freezingAssets.getManager().getId();
		}
		if (StringUtils.isNotBlank(managerId)){

			Member member1 = new Member();
			member1.setP(freezingAssets.getManager());
//			member.setP(member.getManager());
			List<Member> memberList = memberService.findList(member1);
			//查询团队下级人员
			List<Member> queryList = new ArrayList<>();
			//最终集合
			List<Member> list = new ArrayList<>();
			queryList.addAll(memberList);
			list.addAll(memberList);
			while (list.size() > 0) {
				list.clear();
				List<Member> list1 = new ArrayList<>();
				for (Member member2 : queryList) {
					Member entity = new Member();
					entity.setP(member2);
//					member.setP(member2);
					List<Member> memberList1 = memberService.findList(entity);
					memberList.addAll(memberList1);
					list1.addAll(memberList1);
				}
				list.addAll(list1);
				queryList.clear();
				queryList.addAll(list1);
			}
			if (memberList.size() >0){
				for (Member member : memberList) {
					FreezingAssets freezingAssets1 = new FreezingAssets();
					freezingAssets1.setMember(member);
					List<FreezingAssets> list1 = findList(freezingAssets1);
					accountList.addAll(list1);
				}
			}
			page.setCount(accountList.size());
			page.setList(accountList);
		}else {
			List<FreezingAssets> list = findList(freezingAssets);
			page.setList(list);
		}

		return page;
//		return super.findPage(page, freezingAssets);
	}
	
	@Transactional(readOnly = false)
	public void save(FreezingAssets freezingAssets) {
		super.save(freezingAssets);
	}
	
	@Transactional(readOnly = false)
	public void delete(FreezingAssets freezingAssets) {
		super.delete(freezingAssets);
	}

	public FreezingAssets findByMember(Member member, SubGoods subGoods) {
		return mapper.findFreezingAssetsByMember(member,subGoods);
	}

	public List<FreezingAssets> findDaysList(FreezingAssets freezingAssets) {
		return mapper.findDaysList(freezingAssets);
	}

	/**
	 * 查询锁仓资产未申请发货且数量大于0的
	 * @param freezingAssets
	 * @return
	 */
	public List<FreezingAssets> findNonList(FreezingAssets freezingAssets) {
		return mapper.findNonList(freezingAssets);
	}
}